package com.sxt.practiceapplication.activities.course;

import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.TextView;

import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer;
import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.common.ServerStatus;
import com.sxt.practiceapplication.entity.gesture.data.GestureResultData;
import com.sxt.practiceapplication.service.GestureCompareService;
import com.sxt.practiceapplication.utils.CacheManager;
import com.sxt.practiceapplication.utils.GsonBinder;
import com.sxt.practiceapplication.utils.camera.CameraUtil;
import com.sxt.practiceapplication.utils.floatutil.FloatWindow;
import com.sxt.practiceapplication.utils.floatutil.MoveType;
import com.sxt.practiceapplication.utils.floatutil.Screen;
import com.sxt.practiceapplication.utils.video.SampleControlVideo;

import kotlin.Unit;

public class CourseSampleControlVideo extends SampleControlVideo {

    public CameraUtil cameraUtil;

    private TextureView gestureCapture;

    private Context context;

    public boolean isShowCapture;

    public TextView hideOrShowCapture;

    public GestureCompareService gestureCompareService;

    public CourseSampleControlVideo(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public CourseSampleControlVideo(Context context) {
        super(context);
    }

    public CourseSampleControlVideo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init(Context context) {
        this.context = context;
        super.init(this.context);

        initView();

        initCapture();
    }

    private void initView() {
        // 显示/隐藏相机捕捉
        hideOrShowCapture = findViewById(R.id.hide_show_capture);
        hideOrShowCapture.setOnClickListener(v -> {
            if (isShowCapture) {
                FloatWindow.get().hide();
                hideOrShowCapture.setText("显示捕捉");
            } else {
                FloatWindow.get().show();
                hideOrShowCapture.setText("隐藏捕捉");
            }
            isShowCapture = !isShowCapture;
        });
    }

    private void initCapture() {
        gestureCapture = new TextureView(context);

        //初始化相机工具
        cameraUtil = new CameraUtil(context, gestureCapture);

        View gestureTip1 = findViewById(R.id.gesture_tip);
        TextView gestureResult = gestureTip1.findViewById(R.id.gesture_result);
        CacheManager.put("gestureTip", gestureTip1);
        CacheManager.put("gestureResult", gestureResult);
        gestureCompareService = new GestureCompareService(mContext, this, result -> {
            View gestureTip = CacheManager.get("gestureTip");
            gestureTip.setVisibility(VISIBLE);
            GestureResultData gestureResultData = GsonBinder.toObj(result, GestureResultData.class);
            TextView textView = CacheManager.get("gestureResult");
            String code = gestureResultData.getCode();
            if (code.equals(ServerStatus.SUCCESS.getCode())) {
                textView.setText(gestureResultData.getMessage());
            } else {
                textView.setText("未检测到老师或学生的完整姿态");
            }
            return Unit.INSTANCE;
        });
    }

    @Override
    public GSYBaseVideoPlayer startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) {
        FloatWindow.destroy();
        try {
            FloatWindow
                    .with(context.getApplicationContext())
                    .setView(gestureCapture)
                    .setWidth(Screen.width, 0.15f)
                    .setHeight(Screen.width, 0.15f)
                    .setX(Screen.width, 0f)
                    .setY(Screen.height, 0.1f)
                    .setMoveType(MoveType.slide)
                    .setFilter(false)
                    .setMoveStyle(500, new BounceInterpolator())
                    .build();
            FloatWindow.get().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        gestureCompareService.startGestureComparing();
        CourseSampleControlVideo controlVideo = (CourseSampleControlVideo) super.startWindowFullscreen(context, actionBar, statusBar);
        controlVideo.hideOrShowCapture.setVisibility(View.VISIBLE);
        return controlVideo;
    }

    @Override
    protected void cloneParams(GSYBaseVideoPlayer from, GSYBaseVideoPlayer to) {
        super.cloneParams(from, to);
        CourseSampleControlVideo cf = (CourseSampleControlVideo) from;
        CourseSampleControlVideo ct = (CourseSampleControlVideo) to;
        ct.isShowCapture = cf.isShowCapture;
    }

    @Override
    protected void resolveNormalVideoShow(View oldF, ViewGroup vp, GSYVideoPlayer gsyVideoPlayer) {
        super.resolveNormalVideoShow(oldF, vp, gsyVideoPlayer);
        FloatWindow.destroy();
        hideOrShowCapture.setVisibility(View.GONE);
        gestureCompareService.stopGestureComparing();
        if (cameraUtil.isCameraStarted()) {
            cameraUtil.closeCamera();
        }
        View gestureTip = CacheManager.get("gestureTip");
        gestureTip.setVisibility(GONE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.sample_course_video;
    }

}